﻿using System.Text.RegularExpressions;

namespace DynamicChart.Service.FunctionConversion;

public class DefaultConvertStrategy : IConvertStrategy
{
    private readonly string _input;
    private readonly ISqlFunctionAdapter _adapter;

    public DefaultConvertStrategy(string input, ISqlFunctionAdapter adapter)
    {
        _input = input;
        _adapter = adapter;
    }

    public string Convert()
    {
        string pattern = @"^([a-zA-Z_]\w*)\(([^)]*)\)$";
        Match match = Regex.Match(_input, pattern);

        if (match.Success)
        {
            string functionName = match.Groups[1].Value;
            string fieldName = string.IsNullOrEmpty(match.Groups[2].Value) ? ":fieldName" : match.Groups[2].Value; ;

            return _adapter.DefaultConvert(functionName, fieldName);
        }

        return _input;
    }

    public string Convert(string format)
    {
        return Convert();
    }

    public string GenerateWhereClause(string fieldName, string parameterName, string condition = "=")
    {
        string convertedField = _adapter.DateConvert(fieldName);
        return $"{convertedField} {condition} {parameterName}";
    }

    public string GenerateWhereClause(string fieldName, string parameterName, string format, string condition = "=")
    {
        return GenerateWhereClause(fieldName, parameterName, condition);
    }
}